var element;
var layer;
$(function(){
	layui.use(['layer','element'], function(){
	  	layer = layui.layer; 	// 弹层
	  	element = layui.element;// Tab的切换功能，切换事件监听等，需要依赖element模块
	  	
		leftMeunAdd();
		loadDict();
		
	})  	
})

/**
 * 防止页面后退-----------------------------------------------------------------
 */
window.history.go(1);
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

/**
 * TODO
 * 加载所有字典-----------------------------------------------------------------
 */
function loadDict(){
	$.post($projectPath+"/sys/dict/getDictAll",{},function(res){
		if(res.success){
			var map = res.data['dict_map'];
			var list = res.data['dict_list'];
			sessionStorage.setItem("dict_map", JSON.stringify(map));
			sessionStorage.setItem("dict_list", JSON.stringify(list));
		}
	})
}

/**
 * TODO
 * 左侧菜单添加-----------------------------------------------------------------
 */
function leftMeunAdd(){
	$.post($projectPath+"/sys/menu/selectTree",{},function(res){
		if(res.success){//加载成功了
			$("#left_ul").empty();
			if(themeStyle.indexOf("page1") != -1){
				$("#body_1").css("display","none");
				$("#body_2").css("display","block");
				openTabPage(res.data);
			}else{
				$("#body_1").css("display","block");
				$("#body_2").css("display","none");
				openNotTabPage(res.data);
			}
			element.render('nav');
			openLeftMenuItemed();
		}
	})
}

/**
 * 开启标签页----------------------------------------------------------------
 * @returns
 */
function openTabPage(array){
	for (var i = 0; i<array.length; i++) {
		if (array[i].homeState == 1) {
			currentTabId = array[i].url;
			$("#tab_id").append('<li class="layui-this" lay-id="'+array[i].url+'">'+array[i].mname+'</li>');
			$("#home").attr({"src":array[i].url, "name": array[i].url, "id": array[i].url});
		}
		var li = $("<li class='layui-nav-item'></li>");
		var a = $("<a><i class='"+array[i].iconCls+"'></i><cite>"+array[i].mname+"</cite></a>");
		if(array[i].children.length <= 0){//当没有子菜单是当前可以跳转页面
			li = $("<li class='layui-nav-item clickAddMark'></li>");
			if (array[i].homeState == 1) {
				li = $("<li class='layui-nav-item clickAddMark layui-this'></li>");
			}
			a = $("<a href='#"+array[i].url+"' class='a_two' href='javascript:;' lay-direction='2'>" +
					"<i class='"+array[i].iconCls+"' title='"+array[i].mname+"'></i><cite>"+array[i].mname+"</cite></a>");
		}
		$(li).append(a);
		if(array[i].children.length > 0){//存在子菜单
			getMenu1(array[i].children, li, 20);
		}
		$("#left_ul").append(li);
	}
}

/**
 * 递归子菜单-----------------------------------------------------------------
 */
function getMenu1(list,li,left){
	var dl = $("<dl class='layui-nav-child'></div>");
	for (var y = 0; y < list.length; y++) {
		var childList = list[y].children;
		if (list[y].homeState == 1) {
			currentTabId = list[y].url;
			$("#tab_id").append('<li class="layui-this" lay-id="'+list[y].url+'">'+list[y].mname+'</li>');
			$("#home").attr({"src":list[y].url, "name": list[y].url, "id": list[y].url});
		}
		var dd = $("<dd class='layui-nav-item'>");
		var a = $("<a><span style='margin-left: "+left+"px;'><i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		if(childList.length <= 0){
			dd = $("<dd class='clickAddMark'>");
			if (list[y].homeState == 1) {
				dd = $("<dd class='clickAddMark layui-this'>");
			}
			a = $("<a href='#"+list[y].url+"' class='a_two'><span style='margin-left: "+left+"px;'>" +
			 		"<i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		}
		$(dd).append(a);
		if(childList.length > 0){
			getMenu1(childList, dd, (Number(left)+10));
		}
		$(dl).append(dd);
	}
	$(li).append(dl);
}


/**
 * 关闭标签页----------------------------------------------------------------
 * @returns
 */
function openNotTabPage(array){
	for(var i = 0; i<array.length; i++) {
		if (array[i].homeState == 1) {
			$("#homeUrl").attr("src", array[i].url);
		}
		var li = $("<li class='layui-nav-item'></li>");
		var a = $("<a><i class='"+array[i].iconCls+"'></i><cite>"+array[i].mname+"</cite></a>");
		if(array[i].children.length <= 0){//当没有子菜单是当前可以跳转页面
			li = $("<li class='layui-nav-item clickAddMark'></li>");
			if (array[i].homeState == 1) {
				li = $("<li class='layui-nav-item clickAddMark layui-this'></li>");
			}
			a = $("<a href='javascript:reloadIframe(\""+array[i].url+"\");' class='a_two' lay-direction='2'>" +
					"<i class='"+array[i].iconCls+"' title='"+array[i].mname+"'></i><cite>"+array[i].mname+"</cite></a>");
		}
		$(li).append(a);
		if(array[i].children.length > 0){//存在子菜单
			getMenu2(array[i].children, li, 20);
		}
		$("#left_ul").append(li);
	}
}

/**
 * 递归子菜单-----------------------------------------------------------------
 */
function getMenu2(list,li,left){
	var dl = $("<dl class='layui-nav-child'></div>");
	for(var y = 0; y<list.length; y++) {
		var childList = list[y].children;
		if (list[y].homeState == 1) {
			$("#homeUrl").attr("src", list[y].url);
		}
		var dd = $("<dd class='layui-nav-item'>");
		var a = $("<a><span style='margin-left: "+left+"px;'><i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		if(childList.length <= 0){
			dd = $("<dd class='clickAddMark'>");
			if (list[y].homeState == 1) {
				dd = $("<dd class='clickAddMark layui-this'>");
			}
			a = $("<a href='javascript:reloadIframe(\""+list[y].url+"\");' class='a_two'><span style='margin-left: "+left+"px;'>" +
			 		"<i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		}
		$(dd).append(a);
		if(childList.length > 0){
			getMenu2(childList, dd, (Number(left)+10));
		}
		$(dl).append(dd);
	}
	$(li).append(dl);
}

/**
 * 主体界面跳转
 */
function reloadIframe(url) {
	$("#homeUrl").attr("src", url)
}

/**
 * 展开左侧指定父级菜单 ----------------------------------------------------------------------
 */
function openLeftMenuItemed(){
	$('.clickAddMark').each(function(){
		var cls = $(this).attr("class");
		if(cls.indexOf('layui-this') != -1){
			var parentTab = $(this).parents(".layui-nav-item");
			$.each(parentTab, function(i) {
				$(this).addClass("layui-nav-itemed");
			})
		}
	})
}




